﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PreBuildGrupyz.Models.ShoppingCartViewModel>" %>
<%@ Import Namespace="Helpers" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.notifyBar.js" type="text/javascript"></script>
    <link href="../../Content/jquery.notifyBar.css" rel="stylesheet" type="text/css" />

        <script type="text/javascript">

            //var changed = false;

            $(document).ready(function () {
                $('.dropQtd').bind("change", function () {
                    $.post("/ShoppingCart/ActualizaCart", { id: this.id, qtd: this.value }, function (data) {
                        changed = false;
                        $('#cart-status').text('Carrinho (' + data.Quantidade + ')');
                        $('#totalUn-' + data.AddId).text(String(data.ValorUn.toFixed(2)).replace('.', ',') + " €");
                        $('#cart-total').text(String(data.CartTotal.toFixed(2)).replace('.', ',') + " €");
                    });
                });

                //$('.numberonly').numeric('§');
            });

            function handleUpdate(context) {
                    var json = context.get_data();
                    var data = Sys.Serialization.JavaScriptSerializer.deserialize(json);
                    // Update the page elements
                    if (data.LastCartCount == 0)
                        $('#row-' + data.DeleteId).fadeOut('slow');
                    else
                        $('#dropQtd-' + data.DeleteId).val(data.LastCartCount); 
                    $('#cart-status').text('Carrinho (' + data.CartCount + ')');
                    $('#cart-total').text(String(data.CartTotal.toFixed(2)).replace('.', ',') + " €");
                    $('#totalUn-' + data.DeleteId).text(String(data.ValorUn.toFixed(2)).replace('.', ',') + " €");
                    if (data.CartTotal == 0) $('#Dados').hide();

                    $.notifyBar({ html: data.Message,
                        cls: "success"
                    });
                    
                    if (data.CartCount == 0) {
                        window.location.href = '<%: Url.Action("../Home/Index")%>';
                    }

            }
        </script>


      <h3>
        <em>Review</em> your cart:
    </h3>

    <table id="tblCheckout">

        <% foreach (var item in Model.CartItems) { %>
        <tr id="row-<%: item.Id %>">
            <td>
            <%
                if (item.Oferta)
                {
            %>
                <%= Ajax.ImageActionLink("../../Content/images/Gift-Box-icon.png","Detalhe oferta", "DadosOferta", new { id = item.Id },null)%>

            <%
                }
             %>
            </td>
            <td>
                <a href="/Deal/<%: item.DealId +"-"+ item.Deal.Descricao %>"><%: item.Deal.Descricao%></a>
            </td>
            <td>Quantidade</td>
            <td id="qtd">
                <%: Html.DropDownList("dropQtd", new SelectList(Model.ListaItems, "Value", "Text", item.Quantidade), new { id = "dropQtd-" + item.Id, @class = "dropQtd" })%>
            </td>
            <td class="sign">×</td>
            <td id="preco" class="price">
                <%: item.Deal.PrecoDesconto %>
            </td>
             <td class="sign">=</td>

             <td class="total" id="totalUn-<%: item.Id %>"><%: item.Deal.PrecoDesconto * item.Quantidade %> € </td>
            <td>
                <%= Ajax.ImageActionLink("../../Content/images/remove-from-cart-icon_xsmall.jpg","Remover", "RemoveFromCart", new { id = item.Id }, new AjaxOptions { OnSuccess = "handleUpdate" })%>
            </td>
        </tr>
        <% } %>

        <tr class="grand_total">
            <td class="total" colspan="9">
            <label>Total:</label>
             <span id="cart-total" class="grand_total"><%: Model.CartTotal %> €</span>
            </td>
        </tr>

    </table>
    <br />
    <div id="Dados">
    <h2>Os seus dados</h2>
    <br />
        <% if(Request.IsAuthenticated) Html.RenderAction("Checkout", "Checkout"); %>
    </div>

</asp:Content>

